package com.jiguang.property.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiguang.property.dto.BuildInfoDTO;
import com.jiguang.property.entity.BuildInfo;
import com.jiguang.property.entity.HostelInfo;
import com.jiguang.property.mapper.BuildInfoMapper;
import com.jiguang.property.service.BuildInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiguang.property.service.HostelInfoService;
import com.jiguang.property.utils.Result;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2025-01-24
 */
@Service
public class BuildInfoServiceImpl extends ServiceImpl<BuildInfoMapper, BuildInfo> implements BuildInfoService {
    @Autowired
    private HostelInfoService hostelInfoService;

    @Override
    public Result listBuildInfo(BuildInfoDTO buildInfoDTO) {
        //创建分页对象
        Page<BuildInfo> page = new Page<>(buildInfoDTO.getPageNow(),buildInfoDTO.getPageSize());

        QueryWrapper<BuildInfo> wrapper = new QueryWrapper<>();
        //判断楼宇信息是否为空
        if (!StringUtil.isNullOrEmpty(buildInfoDTO.getBuildingName())){
            wrapper.like("building_name",buildInfoDTO.getBuildingName());
        }
        //判断单元是否为空
        if (!StringUtil.isNullOrEmpty(buildInfoDTO.getUnit())){
            wrapper.like("unit",buildInfoDTO.getUnit());
        }
        wrapper.eq("is_delete",0);
        wrapper.orderByDesc("update_time");

        Page<BuildInfo> buildInfoPage = (Page<BuildInfo>) baseMapper.selectPage(page,wrapper);


        return Result.ok().message("查询成功").data("buildInfos", buildInfoPage.getRecords()).data("total",buildInfoPage.getTotal());
    }

    @Override
    public Result deleteBuildInfo(String buildId) {
        if (StringUtils.isEmpty(buildId)) return Result.error().message("Id不能为空");
        List<String> ids = Arrays.asList(buildId.split(","));
        //判断是否存在房屋的楼宇
        for (String id : ids) {
            QueryWrapper<HostelInfo> hWrapper = new QueryWrapper<>();
            hWrapper.eq("build_id",id);
            List<HostelInfo> list = hostelInfoService.list(hWrapper);
            if(!CollectionUtils.isEmpty(list)){
                return Result.error().message("存在关联房屋的楼宇");
            }
        }

        baseMapper.deleteBatchIds(ids);
        return Result.ok().message("删除成功");
    }

    @Override
    public Result updateOrAddBuildInfo(BuildInfoDTO buildInfoDTO) {
        BuildInfo buildInfo = new BuildInfo();
        BeanUtils.copyProperties(buildInfoDTO,buildInfo);

        if (StringUtil.isNullOrEmpty(buildInfo.getId())){
            boolean b = existBuilding(buildInfoDTO);
            if (b) {return Result.error().message("存在该楼宇以及该楼宇的该单元");}
            baseMapper.insert(buildInfo);
        }else{
            boolean b = existBuilding(buildInfoDTO);
            if (b) {return Result.error().message("存在该楼宇以及该楼宇的该单元");}
            baseMapper.updateById(buildInfo);
        }
        return Result.ok().message("成功");
    }

    /**
     * 判断是否存在该楼宇以及该楼宇的该单元
     */
    private boolean existBuilding(BuildInfoDTO buildInfoDTO){
        QueryWrapper<BuildInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("building_name",buildInfoDTO.getBuildingName());
        wrapper.eq("unit",buildInfoDTO.getUnit());
        BuildInfo buildInfo = baseMapper.selectOne(wrapper);

        if (buildInfo != null){
            if (!StringUtils.isEmpty(buildInfoDTO.getId()) && !buildInfo.getId().equals(buildInfoDTO.getId())){
                return true;
            }else if (StringUtils.isEmpty(buildInfoDTO.getId())){
                return true;
            }else{
                return false;
            }
        }

        return false;
    }
}
